package april.fastjson.byte_javabean;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;

public class Serialize {
	// json格式的byte数组和JavaBean直接的转换
	public static final byte[] toJSONStringBytes(Object object,
			com.alibaba.fastjson.serializer.SerializerFeature... features) {
		SerializeWriter out = new SerializeWriter();
		try {
			JSONSerializer serializer = new JSONSerializer(out);
			for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
				serializer.config(feature, true);
			}
			serializer.write(object);
			return out.toBytes("UTF-8");
		} catch (StackOverflowError e) {
			throw new JSONException("maybe circular references", e);
		} finally {
			out.close();
		}
	}
}
